<?php
/*
Author: 
Project: EBBS
*//**********************Description**********************/

include 'library/init.php';
include 'library/header.php';

//Taking POST from commentform
	$sql = 'INSERT INTO Comment (BlogpostID, Username, Text) 
						values (:BlogpostID,:Username, :Text)';
	$sth = $db->prepare($sql);
	$sth->bindParam(':BlogpostID',$_GET['id']);
	$sth->bindParam(':Username', $_POST['Username']);
        $sth->bindParam(':Text', $_POST['Text']);
  	$sth->execute();


//Displays all post from one user
$blogpostid = $_GET['id'];
$userid = $user->getID();

$sql = 'SELECT * FROM blogpost WHERE BlogpostID =:blogpostid';
$sth = $db->prepare($sql);
$sth->bindParam (':blogpostid', $blogpostid);
$sth->execute();
$row = $sth->fetch();
$deleted = $row['Deleted'];
$bUserid = $row['UserID'];

$viewc = $row['ViewCount'] + 1;
            $sql2 = 'UPDATE Blogpost SET ViewCount = :viewc WHERE BlogpostID = :PostID';
            $sth = $db->prepare($sql2);
            $sth->bindParam(':viewc', $viewc);
            $sth->bindParam(':PostID', $blogpostid);
            $sth->execute();


echo "<h1>{$row['Title']}</h1>";
echo $row['Text'];



if($user->loggedOn() && $user->GetID() == 0){
    deletePost($blogpostid);
    echo "<br><a href='admin.php'>Admin</a>";
}
else if($userid == $bUserid){
    if($deleted == 0){
    UserDeletePost($blogpostid);
    echo "<a href='editpost.php?id=$blogpostid'>edit post</a>";
     echo "<form method='post' action='editpost.php?id=$blogpostid'>\n
         <input type='submit' value='Edit Post'/>\n</form>";
    UserDeleteComment($blogpostid);

    }
}

else if($deleted == 0){
    flagPost($blogpostid);

//Displays all comments for a post
echo "<h2>Comments</h2>";
showcomment($blogpostid);	



?>

<!--Form for commenting-->

<form method="post" action="">
<label for="Username">Navn</label>
<input type="text" name="Username"/><br><br>
<label for="Text">Legg inn kommentar her</label><br/>
<textarea name="Text" style="width: 222px; height:100px"></textarea><br/>
<input type="submit" value="Legg inn kommentar"/>
</form>

<?php
}
include 'library/sidebar.php';
include 'library/footer.php';
